import * as types from './mutation-types'
import {setStore,getStore} from "../config/mUtils"
import {geohash} from "./getters";
const  matutaions = {
  /*记录用户信息*/
  [types.RECORD_USERINFO](state,info){
    state.userInfo = info;
    state.login = true;
    setStore('user_id',info.user_id)
  },
  /*获取用户信息存入vuex*/
  [types.GET_USERINFO](state,info){
    /*if (state.userInfo && (state.userInfo.username !== info.username)) {
      return;
    };
    if (!state.login) {
      return
    }
    if (!info.message) {
      state.userInfo = {...info};
    } else {
      state.userInfo = null;
    }*/
    state.userInfo = info;
    state.login = true;
  },
  /*退出登录*/
  [types.OUT_LOGIN](state){
    state.userInfo = {}
    state.login = false
  },
  //修改用户名
  [types.RETSET_NAME](state,username){
    state.userInfo = Object.assign({},state.userInfo,{username})
  },
  //增加地址
  [types.ADD_ADDRESS](state,list){
    state.removeAddress = list
  },
  //通过搜索，添加地址
  [types.SAVE_ADDDETAIL](state,addAddress){
    state.addAddress=addAddress;
  },
  [types.SAVE_ADDRESS](state,address){
    state.removeAddress = address;
  },
  [types.DELETEREMOVEADDRESS](state,list){
    state.removeAddress = list;
  },
  //下单成功，保存订单返回信息
  [types.ORDER_SUCCESS](state,order){
    state.cartPrice = null;
    state.orderMessage = order;
  },
  //会员卡价格纪录
  [types.BUY_CART](state,price){
    state.cartPrice = price;
  },
  //保存当前hash值
  [types.SAVE_GEOHASH](state,geohash){
    state.geohash = geohash;
  },
  // 记录当前经度纬度
  [types.RECORD_ADDRESS](state,{latitude,longitude}){
    state.latitude = latitude;
    state.longitude = longitude;
  },
  // 加入购物车
  [types.ADD_CART](state,{
    shopid,
    category_id,
    item_id,
    food_id,
    name,
    price,
    specs,
    packing_fee,
    sku_id,
    stock
  }){
    let cart = state.cartList;
    let shop = cart[shopid] = (cart[shopid] || {});
    let category = shop[category_id] = (shop[category_id] || {});
    let item = category[item_id] = (category[item_id] || {});
    if (item[food_id]) {
      item[food_id]['num']++;
    } else {
      item[food_id] = {
        "num" : 1,
        "id" : food_id,
        "name" : name,
        "price" : price,
        "specs" : specs,
        "packing_fee" : packing_fee,
        "sku_id" : sku_id,
        "stock" : stock
      };
    }
    state.cartList = {...cart};
    //存入localStorage
    //{"1":
    // {"870":
    //  {"426":
    //   {"1934":
    //    {"num":
    //      1,"id":1934,"name":"四川火锅","price":20,"specs":"","packing_fee":0,"sku_id":1932,"stock":1000}}}}}

    /*payload:Object
    category_id:870
    food_id:1934
    item_id:426
    name:"四川火锅"
    packing_fee:0
    price:20
    shopid:"1"
    sku_id:1932
    specs:""
    stock:1000*/
    setStore('buyCart', state.cartList);
  },
  //移出购物车
  [types.REDUCE_CART](state,{
    shopid,
    category_id,
    item_id,
    food_id,
    name,
    price,
    specs,
  }){
    let cart = state.cartList;
    let shop = (cart[shopid] ||{});
    let category = (shop[category_id] || {});
    let item = (category[item_id] ||{});
    if(item && item[food_id]){
      if(item[food_id]['num']>0){
        item[food_id]['num']--;
        state.cartList = {...cart};
        setStore('buyCart',state.cartList)
      }else{
        //商品数量为0，则清空当前商品的信息
        item[food_id] = null;
      }
    }
  },
    //清空当前商品的购物车信息
  [types.CLEAR_CART](state,shopid){
    state.cartList[shopid] = null;
    state.cartList = {...state.cartList};
    setStore('buyCart',state.cartList)
  },
   //网页初始化时从本地缓存获取购物车数据
  [types.INIT_BUYCART](state){
      let initCart = getStore('buyCart');
      if(initCart){
        state.cartList = JSON.parse(initCart);
      }
  },
   //保存商铺id
  [types.SAVE_SHOPID](state, shopid) {
    state.shopid = shopid;
  },
  ////保存下单后购物id 和 sig
  [types.SAVE_CART_ID_SIG](state,{cart_id,sig}){
    state.cart_id = cart_id;
    state.sig = sig;
  },
  //选择的地址
  [types.CHOOSE_ADDRESS](state, {
    address,
    index
  }) {
    state.choosedAddress = address;
    state.addressIndex = index;
  },
//保存下单参数，用户验证页面调用
  [types.SAVE_ORDER_PARAM](state, orderParam) {
  state.orderParam = orderParam;
},
  //保存下单需要验证的返回值
  [types.NEED_VALIDATION](state, needValidation) {
    state.needValidation = needValidation;
  },
  //下单成功，保存订单返回信息
  [types.ORDER_SUCCESS](state, order) {
    state.cartPrice = null;
    state.orderMessage = order;
  },
  //是否开发票
  [types.CONFIRM_INVOICE](state, invoice) {
    state.invoice = invoice;
  },
  //记录订单页面用户选择的备注, 传递给订单确认页面
  [types.CONFIRM_REMARK](state, {
    remarkText,
    inputText
  }) {
    state.remarkText = remarkText;
    state.inputText = inputText;
  },
  //确认订单页添加新的的地址
  [types.CONFIRM_ADDRESS](state, newAddress) {
    state.newAddress.push(newAddress);
  },
  //选择搜索的地址
  [types.CHOOSE_SEARCH_ADDRESS](state, place) {
    state.searchAddress = place;
  },
  //修改下单参数
  [types.CHANGE_ORDER_PARAM](state, newParam) {
    state.orderParam = Object.assign({}, state.orderParam, newParam);
  },
  //评论列表的图片点击放大
  [types.SHOWBIGIMG](state,flag){
    state.showBigImg = flag;
  },
  //记录店铺详情
  [types.RECORD_SHOPDETAIL](state, detail) {
    state.shopDetail = detail;
  },
  //进入订单详情页前保存该订单信息
  [types.SAVE_ORDER](state, orderDetail) {
    state.orderDetail = orderDetail;
  },
  //保存所选问题标题和详情
  [types.SAVE_QUESTION](state, question) {
    state.question = {...question};
  }
}
export default matutaions
